/*
EVENT GRAPHS

Data 	: 
Folder 	: 
Date	: 2018-03-10

Creator		: Jonas Cederlof	(JC)
Description : 
Notes:


LATEST UPDATE: 	

*/

********************************************************************************

clear
set more		 off
cap   log close 	_all

log using 	"../log/B3_event_RD.log"	, replace 
use 		"$datapath/A3_age55data_WC.dta"


*Keep relevant eventtime
keep if inrange(eventtime_def,-48,48)



*=============
xtset   ym_def
*=============

replace annual_ear= annual_ear/1000

{ // Define globals
*===============================================================================
global covariets 	"C_annual_ear_prenot_def C_female C_immigrant C_tenureatnot C_educ1 C_educ2 C_educ3 "		
global instrument	"treat"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 

{ // Annual earnings
*===============================================================================
eststo clear
matrix B = J(13,4,.)
forvalues t =-48(12)48{
	local j = `j' + 1
	local i = `j' - 5
	eststo :  qui xtreg annual_ear		$instrument $linear $covariets 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`j',1]  = `i'
	matrix B[`j',2]  = _b[_cons]
	matrix B[`j',3]  = _b[_cons] + _b[treat]
	matrix B[`j',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
}
matrix list B
svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-4 4)) xlabel(-4(1)4)  ///
xtitle("Years relative to notification") ytitle("Annual earnings (1,000 SEK)") name(RF1,replace) ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white))) ///
yscale(r(250 400)) ylab(250(50)400)
graph export "$agegraphpath/Event/event_RD_annual_ear.pdf",replace
graph export "$agegraphpath/Event/event_RD_annual_ear.eps",replace as(eps)

drop t C T sig

esttab , keep($instrument _cons)  star(* 0.1 ** 0.05 *** 0.01) 
esttab using "$agetablepath/RF/tab_earnings.tex", 			/// 
replace keep($instrument _cons) booktabs nodepvars nomtitles  			///
varlabels(treat "Above age 55" _cons "Control mean") se(3) b(3)  			///
star(* 0.1 ** 0.05 *** 0.01) refcat( _cons "", nolabel)   			///
stats(r2  N_clust N, fmt( %9.3f %9.0g %9.0g) 					/// 
labels( "$ R^2 $"  "No. of Clusters" "N")) 					///
title("\textsc{RD-estimates on annual earnings} " ) 			///
mlabels("t-4" "t-3" "t-2" "t-1"  "t" "t+1" "t+2" "t+3" "t+4") 					///
nonotes 
}
*

{ // Annual earnings for wage sample (i.e those who have found a job within 2 years)
*===============================================================================
merge m:1 lopnr using "$datapath/wagesample.dta" , nogen

preserve
	keep if inwagesample==1
	
	eststo clear
	matrix B = J(13,4,.)
	local i = 0
	local j = 0
	forvalues t =-48(12)48{
		local j = `j' + 1
		local i = `j' - 5
		eststo : qui xtreg annual_ear		$instrument $linear $covariets 	if eventtime_def==`t'  , cluster(varselid) fe nonest 

		matrix B[`j',1]  = `i'
		matrix B[`j',2]  = _b[_cons]
		matrix B[`j',3]  = _b[_cons] + _b[treat]
		matrix B[`j',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
	}

	svmat B
	rename B1 t
	rename B2 C
	rename B3 T
	rename B4 sig


	*Graph
	twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
	,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-4 4)) xlabel(-4(1)4)  ///
	xtitle("Years relative to notification") ytitle("Annual earnings (1,000 SEK)") name(RF2,replace) ///
	graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white))) ///
	yscale(r(300 450)) ylab(300(50)450)
	graph export "$agegraphpath/Event/event_RD_annual_ear_wagesample.pdf",replace

	drop t C T sig

	esttab , keep($instrument _cons)  star(* 0.1 ** 0.05 *** 0.01) 
	esttab using "$agetablepath/RF/tab_earnings_wagesample.tex", 			/// 
	replace keep($instrument _cons) booktabs nodepvars nomtitles  			///
	varlabels(treat "Above age 55" _cons "Control mean") se(3) b(3)  			///
	star(* 0.1 ** 0.05 *** 0.01) refcat( _cons "", nolabel)   			///
	stats(r2  N_clust N, fmt( %9.3f %9.0g %9.0g) 					/// 
	labels( "$ R^2 $"  "No. of Clusters" "N")) 					///
	title("\textsc{RD-estimates on annual earnings for wage sample} " ) 			///
	mlabels("t-4" "t-3" "t-2" "t-1"  "t" "t+1" "t+2" "t+3" "t+4") 					///
	nonotes 
restore	
}
*

{ // Pr(At firm) with recalls
*===============================================================================

matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)48{
	local i = `i' + 1
	qui xtreg atfirm2		$instrument $linear $covariets 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-12 48)) xlabel(-12(6)48)  ///
xtitle("Months relative to notification") ytitle("Pr(at notifying  firm)") name(a,replace)  ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white)))
graph export "$agegraphpath/Event/event_RD_atfirm.pdf",replace

drop t C T sig
}
*
{ // Pr(Other firm)
*===============================================================================
*Give priority to notifying firm
*rename otherfirm2 otherfirm2_prio
*gen otherfirm2 = atfirm==0 & otherfirm2_prio==1 

matrix B = J(61,4,.)
local i = 0

forvalues t = 0(1)48{
	local i = `i' + 1
	qui xtreg otherfirm2		$instrument $linear $covariets 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
}

svmat B
matrix list B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

*Graph


twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 48)) xlabel(-12(6)48)  ///
xtitle("Months relative to notification") ytitle("Pr(at new firm)") name(b2,replace) ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white)))
graph export "$agegraphpath/Event/event_RD_otherfirm.pdf",replace

drop t C T sig 
}
*
{ // Pr(Non-employed)
*===============================================================================
*Generate: indicator for non-employment
gen nonemp2 = otherfirm2==0 & atfirm==0

matrix B = J(61,4,.)
local i = 0

forvalues t = -12(1)48{
	local i = `i' + 1
	qui xtreg nonemp2		$instrument $linear $covariets 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

*Graph
twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 48)) xlabel(-12(6)48)  ///
yscale(r(0 .4)) ylab(0(.1).4) ///
xtitle("Months relative to notification") ytitle("Pr(non-employment)") name(c,replace) ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white)))
graph export "$agegraphpath/Event/event_RD_nonemp.pdf",replace

drop t C T sig nonemp2

}
*
{ // Pr(At firm) no recalls
*===============================================================================
bys lopnr (eventtime_def) : gen xleavefirm= eventtime_def if atfirm==0  & eventtime_def>=0
bys lopnr (eventtime_def) : egen leavefirm= min(xleavefirm) 
gen NOTleftfirm = atfirm
replace NOTleftfirm = 0 if eventtime_def>=leavefirm

matrix B = J(61,4,.)
local i = 0 
forvalues t = -12(1)48{
	local i = `i' + 1
	qui xtreg NOTleftfirm		$instrument $linear $covariets 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

*Graph
local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 

twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(-12 48)) xlabel(-12(6)48)  ///
xtitle("Months after notification") ytitle("Pr(At notifying firm)") name(a,replace)  ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white)))
graph export "$agegraphpath/Event/event_RD_atfirm_norecalls.pdf",replace

drop t C T sig
}
*

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 

{ // log(wage)
*===============================================================================
*Generate pre wages
*Pre wage from notifying firm (last observred wage at notifying firm before or at notification)
bys lopnr (eventtime_def) : gen nvals3 = _n if manl!=. &  eventtime_def<=0
bys lopnr (eventtime_def) : egen maxnvals3 = max(nvals3)


*Date for last wage at notifying firm
gen xnvals3date = date if nvals3==maxnvals3	& nvals3!=. & inrange(eventtime_def,-24,0)
bys lopnr (eventtime_def) : egen nvals3date = max(xnvals3date)
format nvals3date %tm


*Wage at notifying firm	
gen xprewage = manl if date==nvals3date 
bys lopnr (eventtime_def) : egen prenotfwage = max(xprewage)
drop xprewage

gen diff_log_manl_notfirm_specific = log(manl_notfirm_specific) - log(prenotfwage)
gen diff_log_manl_newfirm = log(manl_newfirm) - log(prenotfwage)

gen logwage = log(manl_notfirm_specific)


xtset ym_def
local outcome "diff_log_manl_notfirm_specific"
matrix B = J(61,4,.)
local i = 0
forvalues t = -12(1)48{
	local i = `i' + 1
	 qui xtreg `outcome'		$instrument $linear $covariets 	if eventtime_def==`t' , cluster(varselid) fe nonest 

	matrix B[`i',1]  = `t'
	matrix B[`i',2]  = _b[_cons]
	matrix B[`i',3]  = _b[_cons] + _b[treat]
	matrix B[`i',4]  = abs(_b[treat]/_se[treat]) >=invttail(e(df_r),0.025)
}

svmat B
rename B1 t
rename B2 C
rename B3 T
rename B4 sig

*Graph

local Tstyle "ms(o) mcolor(black) lcolor(black) lpattern(solid)"
local Cstyle "ms(oh) mcolor(black) lcolor(black) lpattern(dash)"
local Sstyle "lpattern(shortdash_dot) lcolor(gray) vertical" 


twoway (connected T t, `Tstyle') (connected C t, `Cstyle') (rspike T C t if sig==1, `Sstyle') ///
,xline(0, lstyle(foreground) lpattern(dash) lcolor(black)) xscale(r(0 48)) xlabel(-12(6)48)  ///
xtitle("Months relative notification") ytitle("{&Delta}log(wage)") name(b,replace) ///
graphregion(color(white)) legend(order( 1 "Above age 55" 2 "Below age 55" ) region(color(white)))
graph export "$agegraphpath/Event/event_RD_`outcome'_line_covars_withline.pdf",replace
graph export "$agegraphpath/Event/event_RD_`outcome'_line_covars_withline.eps",replace as(eps)

drop t C T sig prenotfwage  diff_log_manl_notfirm_specific *nvals* prenotfwage  logwage




}
*




log close


	
